package cn.tedu.straw.resource.controller;

import cn.tedu.straw.commons.util.R;
import cn.tedu.straw.resource.ex.*;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler({FileUploadException.class})
    public R handleException(Exception e) {
        if (e instanceof FileEmptyException) {
            return R.failure(R.State.ERR_FILE_EMPTY, e);
        } else if (e instanceof FileSizeException) {
            return R.failure(R.State.ERR_FILE_SIZE, e);
        } else if (e instanceof FileTypeException) {
            return R.failure(R.State.ERR_FILE_TYPE, e);
        } else if (e instanceof FileStateException) {
            return R.failure(R.State.ERR_FILE_STATE, e);
        } else if (e instanceof FileUploadIOException) {
            return R.failure(R.State.ERR_FILE_UPLOAD_IO, e);
        }
        e.printStackTrace();
        return R.failure(-999999, "出现了未知错误，请检查控制台！");
    }

}
